package com.dycx.api.controller.manage.food;

import com.dycx.common.RestResponse;
import com.dycx.common.annotation.WriterLog;
import com.dycx.common.enums.BizTypeEnum;
import com.dycx.food.dto.SpecDTO;
import com.dycx.food.dto.SpecValueDTO;
import com.dycx.food.service.SpecFoodService;
import com.dycx.food.service.SpecValueFoodService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping(value = "/manage/food/spec")
public class SpecFoodController {

    @Resource
    private SpecFoodService specService;

    @Resource
    private SpecValueFoodService specValueService;

    /**
     * 添加
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC, bizValue = "{#specDto.specId}")
    @PostMapping("/insert")
    public RestResponse<?> insert(@RequestBody @Valid SpecDTO specDto) {
        return specService.insert(specDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 修改
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC, bizValue = "{#specDto.specId}")
    @PostMapping("/update")
    public RestResponse<?> update(@RequestBody @Valid SpecDTO specDto) {
        return specService.update(specDto) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 刪除
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC, bizValue = "{#specId}")
    @GetMapping("/delete")
    public RestResponse<?> delete(int specId) {
        return specService.delete(specId) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 修改排序
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC, bizValue = "{#specDto.specId}")
    @PostMapping("/updateSort")
    public RestResponse<?> updateSort(@RequestBody SpecDTO specDto) {
        return RestResponse.success(specService.updateSort(specDto));
    }

    /**
     * 查询 根据主键 id 查询
     **/
    @PostMapping("/getPage")
    public RestResponse<?> getPage(@RequestBody SpecDTO specDto) {
        return RestResponse.success(specService.getPage(specDto));
    }

    /**
     * 获取规格值
     **/
    @GetMapping("/getAllListBySpecId")
    public RestResponse<?> getAllListBySpecId(int specId) {
        return RestResponse.success(specValueService.getAllListBySpecId(specId));
    }

    /**
     * 批量保存规格值
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC)
    @PostMapping("/saveSpecValue")
    public RestResponse<?> saveSpecValue(@RequestBody List<SpecValueDTO> list) {
        return specValueService.saveSpecValue(list) ? RestResponse.success() : RestResponse.fail();
    }

    /**
     * 获取商品分类下所有规格
     **/
    @GetMapping("/getSpecByGoodsClassId")
    public RestResponse<?> getSpecByGoodsClassId(int goodsClassId) {
        return RestResponse.success(specService.getSpecByGoodsClassId(goodsClassId));
    }

    /**
     * 添加一个规格值
     **/
    @WriterLog(bizType = BizTypeEnum.MALL_SPEC, bizValue = "{#specValueDto.specId}")
    @PostMapping("/addOneSpecValue")
    public RestResponse<?> addOneSpecValue(@RequestBody SpecValueDTO specValueDto) {
        return RestResponse.success(specValueService.addOneSpecValue(specValueDto));
    }
}
